from pymilvus import (
    connections,
    Collection,
)
import random
from pprint import pprint

connections.connect(
    "default", host="192.168.1.62", port="19530", db_name="zhangrenyang"
)
collection_name = "blog"
try:
    collection = Collection(collection_name)
    # 定义搜索参数配置
    search_params = {"metric_type": "COSINE", "params": {"nprobe": 16}}
    # 生成一个查询向量
    query_vector = [random.random() for _ in range(128)]
    # 执行搜索操作
    results = collection.search(
        data=[query_vector],
        anns_field="embedding",
        param=search_params,
        limit=15,
        output_fields=["id"],
        consistency_level="Bounded",
    )
    pprint(results)
    for hits in results:
        for hit in hits:
            print(f"id:{hit.id},distance:{hit.distance}")
except Exception as e:
    print(e)


# python search code
# res = client.search(
#  collection_name="blog", # Collection name
#  data=query_vector, # Replace with your query vector
#  search_params={
#    "metric_type": "COSINE",
#    "params": {"nprobe":16}, # Search parameters
#  }, # Search parameters
#  limit=15, # Max. number of search results to return
#  output_fields=["id"], # Fields to return in the search results
#  consistency_level="Bounded"
# )
